    //
//  MultipleViewControllerTester.m
//  ModuleTesting
//
//  Created by vo loc on 6/29/11.
//  Copyright 2011 Epsilon Mobile. All rights reserved.
//

#import "MultipleViewControllerTester.h"
#import "ModulesKeyDefines.h"

@implementation MultipleViewControllerTester
@synthesize tableView;

- (void)loadModules:(ModuleInfo*)info
{
	self.title = info.name;
  
  [testers release];
  testers = [[info allDemos] retain];
  
  [moduleInfo release];
  moduleInfo = [info retain];
  
  [tableView reloadData];
}

- (IBAction)close:(id)sender
{
	[self dismissModalViewControllerAnimated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}

- (void)closeChild:(id)sender
{
  if( [self.modalViewController respondsToSelector:@selector(saveSettings)] )
  {
   	[self.modalViewController performSelector:@selector(saveSettings)]; 
  }
  
  [self.modalViewController dismissModalViewControllerAnimated:YES];
  
  [sender removeFromSuperview];
}
- (void)runViewControllerTest:(Class)testerClass
{
  // load view controller 
  UIViewController* viewController;
  
  NSString* nib = NSStringFromClass(testerClass);
  
  // check for nib file existence
  // - if there is no nib file, load using normal init
  // - if there is, use nib file to load
  if( [[NSBundle mainBundle] pathForResource:nib ofType:@"xib"] == nil )
  {
    viewController = [[testerClass alloc] init];
  }
  else {
    // always load with nib
    viewController = [[testerClass alloc] initWithNibName:NSStringFromClass(testerClass) bundle:nil];
  }
  
  [self presentModalViewController:viewController animated:YES];
  [viewController release];
  
  UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
  [[[UIApplication sharedApplication]keyWindow] addSubview:btn];
  [btn release];
  
  btn.backgroundColor = [UIColor blackColor];
  [btn setTitle:@"Close" forState:UIControlStateNormal];
  [btn addTarget:self action:@selector(closeChild:) forControlEvents:UIControlEventTouchUpInside];
  
  if( ![[UIApplication sharedApplication] isStatusBarHidden] )
  {
    btn.frame = CGRectMake(0, 14, 60, 30);
  }
}

#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSString* testerName = [testers objectAtIndex:indexPath.row];
	if( ![self isClassExists:testerName] )
  {
    NSString* format = @"Class %@ not founded";
    NSString* message = [NSString stringWithFormat:format,testerName];
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:message 
                                                    message:nil 
                                                   delegate:nil 
                                          cancelButtonTitle:@"Ok" 
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    
   	return; 
  }                            
  
  Class testerClass = objc_getClass([testerName UTF8String]);
  [self runViewControllerTest:testerClass];
  
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return [testers count];
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString* CellIdentifier = @"Cell";
  UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if( !cell )
  {
  	cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    
  }
  
  cell.text = [testers objectAtIndex:indexPath.row];
  
  return cell;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
